home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programmierung
/
Power-Programmierung (Tewi)(1994).iso
/
qtawk
/
calcinna.exp
< prev
next >
Wrap
Text File
|
1990-09-27
|
8KB
|
281 lines
# QTAwk Utility
# CALCIN - INfix Expression Calculator
#
# Infix Expression Calculator.
# (C) Copyright 1989, 1990 Terry D. Boldt, All Rights Reserved.
#
# Without ANSI.SYS driver use
#
# input: expression in Algebraic form
# output: value of each expression
#
#
BEGIN {
_arg_chk = TRUE;
monthn[ 1] = "January";
monthn[ 2] = "February";
monthn[ 3] = "March";
monthn[ 4] = "April";
monthn[ 5] = "May";
monthn[ 6] = "June";
monthn[ 7] = "July";
monthn[ 8] = "August";
monthn[ 9] = "September";
monthn[10] = "October";
monthn[11] = "November";
monthn[12] = "December";
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
stderr = "stderr";
# set display colors
# highlight
hl_color = "\x01b[0;32;40m";
# normal display
nl_color = "\x01b[1;31;44m";
OFMT = "%.14g";
se_flag = TRUE;
recover_flag = TRUE;
split(sdate(1),tdate,/\//);
today = jdn(tdate[3],tdate[1],tdate[2]);
#
# Gregorian/Julian calender flag.
# TRUE == julian
# FALSE == gregorian
#
greg_jul = FALSE;
numeric = /^({_i}|{_f}|{_r})$/;
Yess = /[Yy](es)?/; # Yes string
Nos = /[Nn]o?/; # No string
Yes = /^{_w}*{Yess}{_w}*$/; # Yes answer
No = /^{_w}*{Nos}{_w}*$/; # No answer
Quit = /^{_w}*[Qq](uit)?({_w}+({Yess}|{Nos}))?{_w}*$/; # define regular expression To Quit
Help = /^{_w}*[Hh](elp)?{_w}*$/; # define regular expression for Help
Stat = /^{_w}*[Ss](tat(us)?)?{_w}*$/; # define regular expression for status
Cls = /^{_w}*[Cc](ls)?{_w}*$/; # define regular expression To Clear Screen
quit_status = TRUE;
# find number of variables defined in calculator progam
# any new variables defined by user will be in addition to this number
# the number, vcnt, is then used in displaying status
vcnt = var_number(FALSE);
copyright;
prompt;
}
GROUP Quit {
if ( NF > 1 ) {
switch ( $2 ) {
case Yes:
quit_status = TRUE;
break;
case No:
quit_status = FALSE;
break;
}
}
exit 2;
}
GROUP Help { help; prompt; next; }
GROUP Stat { stat; prompt; next; }
GROUP Cls { copyright; prompt; next; }
{
line = $0;
if ( $0 !~ /;$/ ) line ∩= ';'; #check for trailing ';'
jj = execute(line,se_flag,recover_flag);
if ( jj ~~ numeric ) jj = addcomma(jj);
print '\t' ∩ jj;
prompt;
}
END {
if ( quit_status ) copyright;
}
# function to convert year/month/day into julian day number
function jdn(year,month,day) {
local yr;
local pfac = 0.6;
local ljdn;
yr = year + (month - 3.0) / 12.0;
ljdn = int(367.0 * yr + pfac) - (2 * int(yr)) + int(yr/4.0)
+ int(day) + 1721117;
if ( !greg_jul ) ljdn += -int(yr/100.0) + int(yr/400.0) + 2;
return ljdn;
}
# function to convert julian dday number to year/month/day
function caln(cjdn) {
local n, ic, np, npp, mp;
local yr, mo, day;
local wkd = weekday[(cjdn + 1) % 7];
n = int(cjdn) - 1721119;
ic = int((n - 0.2)/36524.25);
if ( greg_jul ) np = n + 2; else np = n + ic - (ic / 4);
yr = int((np - 0.2)/365.25);
npp = np - int(365.25 * yr);
mp = int((npp - 0.5)/30.6);
day = int(npp + 0.5 - 30.6 * mp);
if ( mp <= 9 ) mo = mp + 3;
else {
yr++;
mo = mp - 9;
}
return wkd ∩ ", " ∩ day ∩ " " ∩ monthn[mo] ∩ ", " ∩ yr;
}
# function to set stack to today + days in future (past)
function fdate(days) {
local tdate; # tdate[1] == month, tdate[2] == day, tdate[3] = year
local fd;
local wkday;
fd = today + days;
return caln(fd);
}
# function to provide header & copyright information
function copyright() {
fprintf(stderr,"\n");
fprintf(stderr,"Infix Calculator\n(C) Copyright 1989, 1990 Terry D. Boldt, All Rights Reserved\n");
}
function help() {
copyright;
fprintf(stderr,"Operators Available:\n");
fprintf(stderr,"n1 + - * / n2, add subtract multiply divide n1 to n2\n");
fprintf(stderr,"n1 % n2, n1 remainder of n2\n");
fprintf(stderr,"n1 ^ n2, n1 to n2 power\n");
fprintf(stderr,"n1 [& | @] n2, n2 bit-wise [ and or xor ] n2\n");
fprintf(stderr,"n1 [<< >>] n2, n1 shifted [ left right ] n2 bits\n");
fprintf(stderr,"atan2(n1,n2), arc_tan(n1/n2), -π to π\n");
fprintf(stderr,"~n1, one's complement of n1\n");
fprintf(stderr,"var, display value of variable var\n");
fprintf(stderr,"built-in single argument functions:\n");
fprintf(stderr,"sin asin cos acos sinh cosh log log10 int exp sqrt fract\n");
fprintf(stderr,"jdn(yr,mo,day) - compute julian day number from date\n");
fprintf(stderr,"dow(yr,mo,day) - compute day of week from from date, Sunday == 0\n");
fprintf(stderr,"caln(jdn) - compute date from julian day number\n");
fprintf(stderr,"fdate(days) - compute date days into future(past)\n");
fprintf(stderr,"hrs(hh,mm,ss) - compute hours from hour, minute, second\n");
fprintf(stderr,"hms(hr) - compute hour, minute, second from hours\n");
fprintf(stderr,"now() or now - return current time, decimal\n");
fprintf(stderr,"\n[Hh](elp)? to display help\n");
fprintf(stderr,"[Ss](tatus)? to display status\n");
fprintf(stderr,"[Cc](ls)? to clear screen\n");
fprintf(stderr,"[Qq](uit)? to quit\n");
}
# function to display calculator status
function stat() {
local j, jj, ostr;
copyright;
fprintf(stderr,"Calculator Status:\n");
printf("Calender Set: %s.\n",greg_jul ? "Julian" : "Gregorian");
fprintf(stderr,"Assume %s for Trig. Functions\n",DEGREES ? "Degrees" : "Radians");
fprintf(stderr,"Defined variables:\n");
fprintf(stderr,"Pre-Defined:\n");
fprintf(stderr,"se_flag == %s\n",se_flag ? "TRUE" : "FALSE");
fprintf(stderr,"recover_flag == %s\n",recover_flag ? "TRUE" : "FALSE");
fprintf(stderr,"today == %s\n",addcomma(today));
fprintf(stderr,"User-Defined:\n");
for ( j = vcnt , ostr = ud_sym(j++,jj) ; jj ; ostr = ud_sym(j++,jj) ) {
if ( ostr ~~ numeric ) ostr = addcomma(ostr);
fprintf(stderr,"%s == %s\n",jj,ostr);
}
}
# function to convert hours, minutes, seconds to fractional hours
function hrs(hrs,min,sec) {
return hrs += min/60.0 + sec/3600.0;
}
# functions to convert fractional hours to hrs, min, sec
function hms(hrs) {
local hr = int(hrs);
local mins = fract(hrs) * 60;
local secs = fract(mins) * 60;
local ampm;
mins = int(mins);
if ( secs > 59 ) {
secs = 0;
mins++;
} else secs = int(secs);
if ( mins > 59 ) {
mins = 0;
hr++;
}
if ( hr >= 24 ) hr = 0;
if ( hr < 12 ) ampm = "am";
else {
ampm = "pm";
if ( hr > 12 ) hr -= 12;
}
return hr ∩ ":" ∩ mins ∩ ":" ∩ secs ∩ " (" ∩ ampm ∩ ")";
}
# function to add commas to numbers
function addcomma(x) {
local num;
local spat;
local bnum = /{_d}{3,3}([,.]|$)/;
if ( x < 0 ) return "-" addcomma(-x);
num = sprintf("%.14g",x); # num is dddddd.dd
spat = num ~~ /\./ ? /{_d}{4,4}[.,]/ : /{_d}{4,4}(,|$)/;
while ( num ~~ spat ) sub(bnum,",&",num);
return num;
}
function prompt() {
printf("<>");
}
# function to return the current number of GLOBAL variables defined in utility
function var_number(display) {
local cnt, j, jj;
for ( jj = cnt = 1 ; jj ; cnt++ ) {
j = ud_sym(cnt,jj);
if ( display ) print cnt ∩ " : " ∩ jj ∩ " ==>" ∩ j ∩ "<==";
}
return cnt - 1;
}
# function to compute day of week from date
function dow(yr,mo,day) {
return (jdn(yr,mo,day) + 1) % 7;
}
# function return current time, decimal
function now() {
local ntime;
split(stime(1),ntime,/:/);
return hrs(ntime[1] + 0.0,ntime[2] + 0.0,ntime[3] + 0.0);
}